{% extends "personal_center/base.html" %}

{% block page_link %}
    <link rel="stylesheet" href="../../static/system_setup/css/style.css">
    <link rel="stylesheet" type="text/css" href="../../static/personal_center/css/index.css">
    <link rel="stylesheet" type="text/css" href="../../static/personal_center/css/common.css">
    <script type="text/javascript" src="../../static/personal_center/js/index.js"></script>
{% endblock %}


{% block style %}
    <style>
        .add_class {
            background: #1E90FF;
        }
        .top_tag {
            border-bottom: #01AAED solid 2px;color: #01AAED;
        }
        .tab_top {
            width: 100px;height: 40px;line-height: 40px;text-align: center;display: inline-block;cursor: pointer;
        }
        .file {
            position: relative;display: inline-block;background: #D0EEFF;border: 1px solid #99D3F5;border-radius: 4px;padding: 4px 12px;overflow: hidden;color: #1E88C7;text-decoration: none;text-indent: 0;line-height: 20px;
        }
        .file input {
            position: absolute;font-size: 100px;right: 0;top: 0;opacity: 0;
        }
        .file:hover {
            background: #AADFFD;border-color: #78C3F3;color: #004974;text-decoration: none;
        }
        .input-style {
            display: inline-block;width: 100%;height: 35px;font-size: 14px;border: none;background: white;
        }
    </style>
{% endblock %}


{% block right_content %}
    <div style="width: 100%;height: 40px;background: white;border-bottom: #A9A9A9 solid 0.5px;line-height: 40px;padding-left: 1%;color: black;">
        <div id="DW_tag" class="tab_top" onclick="change_mind('DW')">
            来文拟稿
        </div>
        <div id="summit_button" style="height: 100%;display: inline-block;float: right;padding-right: 15px;">
            <button id="summ_id" onclick="summit_fun()" style="width: 100px;height: 30px;margin-top: 5px;border-radius: 3px;border: #1E90FF solid 1px;color: white;background-color: #1E90FF;line-height: 25px;">提交</button>
        </div>
    </div>

    <div id="DW" style="width: 100%;height: 95%;background: white;">
        <div style="width: 100%;height: 40px;line-height: 50px;text-align: center;">
            <h3 id="manuscript_title" style="line-height: 50px;">电 文 处 理 及 阅 文 笺</h3>
        </div>
        <hr>
        <div style="width: 90%;margin-left: 5%;height: 90%;overflow: auto;margin-top: 1%;border-top: #cccccc solid 1px;">
            <div style="width: 100%;">
                <table style="width: 100%;">
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">接收人</p></td>
                        <td id="ngr" style="width: 35%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;">{{ realname }}</td>
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">接收科室</p></td>
                        <td id="ngks" style="width: 35%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;">{{ department }}</td>
                    </tr>
                    <tr style="width: 100%">
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">收文编号</p></td>
                        <td id="swbh" style="width: 35%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;">{{ lw_count }}</td>
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><span style="color: red;">* </span><p style="width: 65px;display: inline-block;text-align-last: justify;">收文日期</p></td>
                        <td id="swrq" style="width: 35%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;">
                            <input class="input-style" type="text" name="car_start" id="car_start" autocomplete="off">
                        </td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">分管领导</p></td>
                        <td style="width: 35%;height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;">
                            {% for foo in leaders_obj %}
                                <div style="width: 70px;display: inline-block;vertical-align: top;"><input type="checkbox" onclick="multiple_choice(this)" class="leader_charge" value="{{ foo.id }}"><p style="width: 45px;display: inline-block;text-align-last: justify;vertical-align: top;margin-left: 3px;">{{ foo.real_name }}</p></div>
                            {% endfor %}
                        </td>
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">阅示领导</p></td>
                        <td style="width: 35%;height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;">
                            {% for foo in leaders_obj %}
                                <div style="width: 70px;display: inline-block;vertical-align: top;"><input type="checkbox" class="read_leadership" value="{{ foo.id }}"><p style="width: 45px;display: inline-block;text-align-last: justify;vertical-align: top;margin-left: 3px;">{{ foo.real_name }}</p></div>
                            {% endfor %}
                        </td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><span style="color: red;">* </span><p style="width: 65px;display: inline-block;text-align-last: justify;">来文字号</p></td>
                        <td id="lwzh" style="width: 35%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true"></td>
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><span style="color: red;">* </span><p style="width: 65px;display: inline-block;text-align-last: justify;">通知类</p></td>
                        <td style="width: 35%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;"><div style="width: 65px;display: inline-block;"><input type="radio" name="notification" value="1" class="notification_class"> 是</div><div style="width: 65px;display: inline-block;"><input type="radio" name="notification" value="0" class="notification_class" checked> 否</div></td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;line-height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><span style="color: red;">* </span><p style="width: 65px;display: inline-block;text-align-last: justify;">来文单位</p></td>
                        <td id="lwdw" colspan="3" style="width: 85%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true"></td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><span style="color: red;">* </span><p style="width: 65px;display: inline-block;text-align-last: justify;">标题</p></td>
                        <td id="gwbt" colspan="3" style="width: 85%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true"></td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><span style="color: red;">* </span><p style="width: 65px;display: inline-block;text-align-last: justify;">内容</p></td>
                        <td id="gwnr" colspan="3" style="width: 85%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true"></td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">标注文本</p></td>
                        <td id="bzwb" colspan="3" style="width: 85%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true"></td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">时间期限</p></td>
                        <td id="sjqx" colspan="3" style="width: 85%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true"></td>
                    </tr>
                    <tr style="width: 100%;">
                        <td style="width: 15%;height: 40px;border: #cccccc solid 1px;text-align: right;padding-right: 1%;border-top: none;"><p style="width: 65px;display: inline-block;text-align-last: justify;">备注</p></td>
                        <td id="beizhu" colspan="3" style="width: 85%;height: 40px;line-height: 40px;border: #cccccc solid 1px;border-left: none;padding-left: 1%;border-top: none;outline: none;" contenteditable="true">

                        </td>
                    </tr>
                </table>
            </div>
            <div style="width: 100%;">
                <div style="width: 100%;height: 40px;line-height: 40px;background-color: #F5F5F5;color: #1E90FF;padding-left: 1%;border: #cccccc solid 1px;border-top: none;border-bottom: none;padding-right: 1%;">
                    <span style="color: red;">* </span>上传文件
                    <a href="javascript:;" class="file" style="vertical-align: top;margin-top: 5px;float: right;">
                        上传文件
                        <input type="file" name="regular_main_body" id="regular_main_body" onchange="upload_accessory('file1', 'regular_main', 'regular_main_body')">
                    </a>
                </div>
                <table style="width: 100%;">
                    <thead>
                        <tr>
                            <td style="width: 5%;height: 35px;border: #cccccc solid 1px;text-align: center;">序号</td>
                            <td style="width: 20%;height: 35px;border: #cccccc solid 1px;border-left: none;text-align: center;">文件名称</td>
                            <td style="width: 20%;height: 35px;border: #cccccc solid 1px;border-left: none;text-align: center;">文件大小</td>
                            <td style="width: 20%;height: 35px;border: #cccccc solid 1px;border-left: none;text-align: center;">上传时间</td>
                            <td style="width: 20%;height: 35px;border: #cccccc solid 1px;border-left: none;text-align: center;">上传人员</td>
                            <td style="width: 15%;height: 35px;border: #cccccc solid 1px;border-left: none;text-align: center;">操作</td>
                        </tr>
                    </thead>
                    <tbody style="overflow: auto;" id="regular_main"></tbody>
                </table>
            </div>
        </div>
    </div>
{% endblock %}


{% block script %}
    <script type="text/javascript" src="../../static/administrative/js/other/jquery.js"></script>
    <script src="../../static/administrative/layDate-v5.0.9/laydate/laydate.js"></script> <!-- 改成你的路径 -->
    <script type="text/javascript" src="../../static/administrative/js/layer.js"></script>
    <script>
        window.onload = function () {
            document.getElementById("sponsor").className = "add_class";
            document.getElementById("personage").className = "add-nav-active";
        };

        const uuid = "{{ uuid | safe }}"

        lay('#version').html('-v' + laydate.v);
        //执行一个laydate实例
        laydate.render({
            elem: '#car_start' //指定元素
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#car_end' //指定元素
        });

        function multiple_choice(ele_tag) {
            var leader_charge_list = document.getElementsByClassName('leader_charge');
            for (var i=0; i<leader_charge_list.length; i++) {
                leader_charge_list[i].checked = false
            }
            ele_tag.checked = true;
        }

        function upload_accessory(file_type, tbody_id, accessory_type) {
            document.getElementById("loading").style.display = "inline-block";
            var swbh = document.getElementById("swbh").innerText;
            var form = new FormData();
            var files = document.getElementById(accessory_type).files[0];
            form.append(file_type, files);
            form.append('number', uuid);
            form.append("source", "lw")
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/topics_attachments_upload", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        creat_table(JsonDate, tbody_id)
                    }
                }
            };
        }

        var zhengwen = {{ zhengwen | safe }};
        var fujian = {{ fujian | safe }};
        for (var zw=0; zw<zhengwen.length; zw++){
            creat_table(zhengwen[zw], "regular_main");
        }
        for (var fj=0; fj<fujian.length; fj++){
            creat_table(fujian[fj], "regular_accessory");
        }

        // 创建附件列表
        function creat_table(JsonData, tbody_id) {
            var t_table = document.getElementById(tbody_id);
            var local_host = window.location.host;
            if (Array.isArray(JsonData["pdf_path"])){
                for (var i=0; i<JsonData["pdf_path"].length; i++){
                    var tr = document.createElement("tr");
                    var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
                    td1.innerHTML = t_table.children.length+1;td1.style.textAlign = "center";td1.style.border="#cccccc solid 1px";td1.style.borderTop="none";td1.style.height="35px";
                    td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border="#cccccc solid 1px";td2.style.borderLeft="none";td2.style.borderTop="none";td2.style.height="35px";
                    td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border="#cccccc solid 1px";td3.style.borderLeft="none";td3.style.borderTop="none";td3.style.height="35px";
                    td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border="#cccccc solid 1px";td4.style.borderLeft="none";td4.style.borderTop="none";td4.style.height="35px";
                    td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border="#cccccc solid 1px";td5.style.borderLeft="none";td5.style.borderTop="none";td5.style.height="35px";
                    td6.innerHTML = "<a target='_blank' href='"+JsonData["pdf_path"][i]["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \""+tbody_id+"\", \""+JsonData["pdf_path"][i]["id"]+"\")'>删除</a>";td6.style.textAlign = "center";td6.style.border="#cccccc solid 1px";td6.style.borderLeft="none";td6.style.borderTop="none";td6.style.height="35px";
                    tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                    t_table.appendChild(tr);
                }
                if (JsonData["pdf_path"].length > 1){
                    document.getElementById("del_adjunct").style.display='block';
                }
            }else {
                tr = document.createElement("tr");
                td1 = document.createElement("td");td2 = document.createElement("td");td3 = document.createElement("td");td4 = document.createElement("td");td5 = document.createElement("td");td6 = document.createElement("td");
                td1.innerHTML = t_table.children.length+1;td1.style.textAlign = "center";td1.style.border="#cccccc solid 1px";td1.style.borderTop="none";td1.style.height="35px";
                td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border="#cccccc solid 1px";td2.style.borderLeft="none";td2.style.borderTop="none";td2.style.height="35px";
                td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border="#cccccc solid 1px";td3.style.borderLeft="none";td3.style.borderTop="none";td3.style.height="35px";
                td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border="#cccccc solid 1px";td4.style.borderLeft="none";td4.style.borderTop="none";td4.style.height="35px";
                td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border="#cccccc solid 1px";td5.style.borderLeft="none";td5.style.borderTop="none";td5.style.height="35px";
                td6.innerHTML = "<a target='_blank' href='"+JsonData["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \""+tbody_id+"\", \""+JsonData["id"]+"\")'>删除</a>";td6.style.textAlign = "center";td6.style.border="#cccccc solid 1px";td6.style.borderLeft="none";td6.style.borderTop="none";td6.style.height="35px";
                tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                t_table.appendChild(tr);
            }
        }

        function summit_fun() {
            var charge_flag = "{{ charge_flag | safe }}";
            var leaders_people = "";var read_leadership_list = "";
            var swbh = document.getElementById("swbh").innerText;           // 收文编号
            var lwdw = document.getElementById("lwdw").innerText;           // 来文单位
            var lwzh = document.getElementById("lwzh").innerText;           // 来文字号
            var swrq = document.getElementById("car_start").value;          // 收文日期
            var gwbt = document.getElementById("gwbt").innerText;           // 收文标题
            var gwnr = document.getElementById("gwnr").innerText;           // 收文内容
            var bzwb = document.getElementById("bzwb").innerText;           // 标注文本
            var sjqx = document.getElementById("sjqx").innerText;           // 时间期限
            var beizhu = document.getElementById("beizhu").innerText;       // 来文备注
            var notification = $("input[type=radio][name=notification]:checked").val(); // 通知类

            var LeaderInCharge = document.getElementsByClassName("leader_charge");
            for (var kk=0; kk<LeaderInCharge.length; kk++) {
                if (LeaderInCharge[kk].checked) {
                    leaders_people = leaders_people + LeaderInCharge[kk].value + ','
                }
            }

            var ReadLeadershipList = document.getElementsByClassName("read_leadership")
            for (var tt=0; tt<ReadLeadershipList.length; tt++) {
                if (ReadLeadershipList[tt].checked) {
                    read_leadership_list = read_leadership_list + ReadLeadershipList[tt].value + ','
                }
            }

            if (!lwdw) {
                alert("来文单位不能为空！")
                return false
            } else if (!lwzh) {
                alert("来文字号不能为空！")
                return false
            } else if (!swrq) {
                alert("收文日期不能为空！")
                return false
            } else if (!gwbt) {
                alert("标题不能为空！")
                return false
            } else if (!gwnr) {
                alert("内容不能为空！")
                return false
            }

            if (charge_flag === "True" && notification === '0') {
                if (leaders_people === "") {
                    alert("请选择分管院长！")
                    return false
                }
            }

            if (notification === '1') {
                if (read_leadership_list === "") {
                    alert("请选择阅示领导！")
                    return false
                }
            }

            var t_table = document.getElementById('regular_main').childNodes.length;
            if (t_table === 0) {
                alert("请上传正文！")
                return false
            }

            document.getElementById("loading").style.display = "inline-block";
            var send_str = "send_number="+String(lwzh)+"&recive_number="+swbh+"&send_company="+lwdw+"&recive_time="+swrq+"&title="+gwbt+"&content="+gwnr+"&annotated_text="+bzwb+"&time_limit="+sjqx+"&uuid="+uuid+"&beizhu="+beizhu+"&leaders_people="+leaders_people+"&charge_flag="+charge_flag+"&read_leadership_list="+String(read_leadership_list)+"&notification="+String(notification);
            var xhr = new XMLHttpRequest();
            xhr.open('post', "/administrative/official_documents_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send(send_str);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("创建成功！")
                        window.location.href = "/personal-center/OfficialDocumentSubmission.html"
                    }
                }
            };
        }

        function RemoveTr(_this, tbody_id, wj_id) {
            var table = document.getElementById(tbody_id);
            var trNode = $(_this).parent().parent();
            trNode.remove();

            for (var i=0; i<table.children.length; i++){
                table.children[i].children[0].innerHTML=i+1;
            }

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/delete_attachments_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id="+String(wj_id));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("删除成功！")
                    }
                }
            };
        }
    </script>
{% endblock %}
